#!/usr/bin/env python3
# encoding: utf-8
# copyright=zheng panyong
# version: xlsx csv tool 1.0

import pandas as pd
import os
import sys
     
    
#数据源Excel如果有多个sheet，那么则需要一一遍历输出
def xlsdata_to_csv(xlsx_data,tag_path):
    for K, V in xlsx_data.items():
        tag_file = os.path.join(tag_path, K + '.csv')
        if not V.empty:
            V.to_csv(tag_file, encoding='utf_8_sig')

#将路径下csv文件夹*.xlsx_csv_dir(sheet.csv)转化回*.xlsx
def dealDir(source_path):
    dir = os.listdir(source_path)
    for i in dir :
        source_file = os.path.join(source_path,i)
        if source_file.endswith(".xlsx_csv_dir"):
            print("source_file:", source_file)
            xlsx_path = os.path.join(source_path, i.split('.xlsx_csv_dir')[0] + "2.xlsx")
            print("xlsx_path:", xlsx_path)
            if os.path.exists(xlsx_path):
                os.remove(xlsx_path)
                print("rm old ", xlsx_path)

            csv_source_path = source_file
            dircsv = os.listdir(csv_source_path)
            for j in dircsv:
                csv_file = os.path.join(csv_source_path, j)
                print("csv_file:", csv_file)
                if csv_file.endswith(".csv"):
                    csv_name = j.split('.csv')[0]
                    csv = pd.read_csv(csv_file, encoding='utf-8')
                    if os.path.exists(xlsx_path):
                        with pd.ExcelWriter(xlsx_path, mode='a', engine='openpyxl') as writer:
                            csv.to_excel(writer, sheet_name=csv_name, index = False)
                        print("exits xlsx add csv sheet:", csv_name)
                    else:
                        print("create xlsx add csv sheet:", csv_name)
                        csv.to_excel(xlsx_path, sheet_name=csv_name, index = False)

#传入xlsx路径，将csv转化成xlsx
def dealFile(source_path):
    xlsx_path = source_path
    csv_path = source_path.split('.xlsx')[0] + ".xlsx_csv_dir"
    if os.path.exists(csv_path):
        if os.path.exists(xlsx_path):
            os.remove(xlsx_path)
            print("csv exit rm xlsx:", xlsx_path)
        csv_source_path = csv_path
        dircsv = os.listdir(csv_source_path)
        for j in dircsv:
            csv_file = os.path.join(csv_source_path, j)
            print("csv_file:", csv_file)
            if csv_file.endswith(".csv"):
                csv_name = j.split('.csv')[0]
                csv = pd.read_csv(csv_file, encoding='utf-8')
                if os.path.exists(xlsx_path):
                    with pd.ExcelWriter(xlsx_path, mode='a', engine='openpyxl') as writer:
                        csv.to_excel(writer, sheet_name=csv_name, index = False)
                    print("exits xlsx add csv sheet:", csv_name)
                else:
                    print("create xlsx add csv sheet:", csv_name)
                    csv.to_excel(xlsx_path, sheet_name=csv_name, index = False)

#传入xlsx路径，判断xlsx存在，且csv_dir存在，则删除xlsx
def delxlsx(xlsx_path):
    csv_path = xlsx_path.split('.xlsx')[0] + ".xlsx_csv_dir"
    if os.path.exists(csv_path):
        print("xlsx_path:", xlsx_path)
        if os.path.exists(xlsx_path):
            os.remove(xlsx_path)
            print("csv_path exit rm xlsx_path ")
            
    
if __name__ == '__main__':
    source_path = ''  #存放csv文件
    try:
        print("第一个传入的参数为:", sys.argv[1])
        source_path = sys.argv[1]
    except Exception as e:
        print("Input Error:", e)
    
    dealFile(source_path)


